﻿Imports System.IO

Public Class ToolsForm

#Region "Event handling"

    Private Sub btnSluiten_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSluiten.Click
        Me.Close()
    End Sub

    Private Sub llResetVoorraad_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs) Handles llResetVoorraad.LinkClicked
        If (MessageBox.Show("De voorraad van alle artikelen wordt op 0 gezet. Ben je zeker?", "Bevestiging", _
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then Return
        Try
            Dim aantal As Integer = ToolsDal.ZetVoorraadOpNul()
            Utility.ResetData()
            ToonInfo(String.Format("{0} artikels werden bijgewerkt", aantal))
            llResetVoorraad.Enabled = False
        Catch ex As Exception
            ToonFout(ex.ToString())
        End Try
    End Sub

    Private Sub llKopieerVoorraad_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs) Handles llKopieerVoorraad.LinkClicked
        If (MessageBox.Show("De verwachte voorraad van alle artikelen wordt op bijgewerkt. Ben je zeker?", "Bevestiging", _
                            MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then Return
        Try
            Dim aantal As Integer = ToolsDal.KopieerVoorraadNaarVerwachteVoorraad()
            Utility.ResetData()
            ToonInfo(String.Format("{0} artikels werden bijgewerkt", aantal))
            llResetVoorraad.Enabled = False
        Catch ex As Exception
            ToonFout(ex.ToString())
        End Try
    End Sub

    Private Sub btnBrowseBron_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBrowseBron.Click
        If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
            tbImportSource.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub btnBrowseTarget_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBrowseTarget.Click
        If (OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK) Then
            tbImportTarget.Text = OpenFileDialog1.FileName
        End If
    End Sub

    Private Sub cbVoorraad_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbVoorraad.CheckedChanged
        RefreshButtonState()
    End Sub

    Private Sub cbVerwachteVoorraad_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbVerwachteVoorraad.CheckedChanged
        RefreshButtonState()
    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles rbArtikels.CheckedChanged, rbVerkopen.CheckedChanged, rbLeveringen.CheckedChanged
        RefreshButtonState()
    End Sub

    Private Sub btnImporteren_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnImporteren.Click
        If (rbArtikels.Checked) Then ImportArtikels()
        If (rbLeveringen.Checked) Then ImportLeveringen()
        If (rbVerkopen.Checked) Then ImportVerkopen()
        Utility.ResetData()
    End Sub

    Private Sub btnScriptLaden_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnScriptLaden.Click
        If (Me.OpenScriptDialog.ShowDialog() = DialogResult.OK) Then
            Using stream As FileStream = File.Open(Me.OpenScriptDialog.FileName, FileMode.Open)
                Dim reader As TextReader = New StreamReader(stream)
                tbScript.Text = reader.ReadToEnd()
                stream.Close()
            End Using
        End If
    End Sub

    Private Sub btnScriptUitvoeren_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnScriptUitvoeren.Click
        tbInfo.Text = ToolsDal.ExecuteScripts(tbScript.Lines)
        Utility.ResetData()
    End Sub

    Private Sub btnBekijkenImport_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBekijkenImport.Click
        ' TODO
    End Sub

    Private Sub ButtonBeheerMeldingen_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ButtonBeheerMeldingen.Click
        Dim f As New BeheerMeldingenForm()
        f.ShowDialog()
    End Sub

#End Region

#Region "Private methods"

    Private Sub ImportArtikels()
        Try
            ToonInfo(ToolsDal.ImportArtikels(tbImportSource.Text, tbImportTarget.Text, cbVoorraad.Checked, cbVerwachteVoorraad.Checked))
        Catch ex As Exception
            ToonFout(ex.ToString())
        End Try
    End Sub

    Private Sub ImportLeveringen()
        Try
            Dim f As New SelecteerLeveringForm(tbImportSource.Text)
            If (f.ShowDialog = Windows.Forms.DialogResult.OK) Then
                Dim idLevering As Integer = f.IdLevering
                ToonInfo(ToolsDal.ImportLevering(tbImportSource.Text, tbImportTarget.Text, idLevering, _
                    cbVoorraad.Checked, cbVerwachteVoorraad.Checked))
            End If
        Catch ex As Exception
            ToonFout(ex.ToString())
        End Try
    End Sub

    Private Sub ImportVerkopen()
        Try
            Dim f As New SelecteerVerkoopForm(tbImportSource.Text)
            If (f.ShowDialog() = Windows.Forms.DialogResult.OK) Then
                Dim idVerkoop As Integer = f.IdVerkoop
                ToonInfo(ToolsDal.ImportVerkoop(tbImportSource.Text, tbImportTarget.Text, idVerkoop, _
                    cbVoorraad.Checked, cbVerwachteVoorraad.Checked))
            End If
        Catch ex As Exception
            ToonFout(ex.ToString())
        End Try
    End Sub

    Private Sub RefreshButtonState()
        btnImporteren.Enabled = (cbVoorraad.Checked Or cbVerwachteVoorraad.Checked) And _
            (rbArtikels.Checked Or rbLeveringen.Checked Or rbVerkopen.Checked)
        ' TODO: ook databanken moeten allebei ingevuld zijn
    End Sub

    Private Sub ToonInfo(ByVal msg As String)
        tbInfo.AppendText(msg)
        tbInfo.AppendText(Environment.NewLine)
        tbInfo.ScrollToCaret()
    End Sub

    Private Sub ToonFout(ByVal msg As String)
        Dim startPos As Integer = tbInfo.Text.Length
        tbInfo.SelectionStart = tbInfo.Text.Length
        tbInfo.AppendText(msg)
        Dim endPos As Integer = tbInfo.Text.Length
        tbInfo.Select(startPos, endPos - startPos)
        tbInfo.SelectionColor = Color.Red
        tbInfo.AppendText(Environment.NewLine)
        tbInfo.ScrollToCaret()
    End Sub

#End Region

End Class
